package com.wyt.cloud.utils.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wyt.cloud.utils.query.duty.QueryChainPipeline;
import com.wyt.cloud.utils.query.duty.QueryTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

@Component
public class WrapperHelper {

    @Autowired
    private QueryChainPipeline queryChainPipeline;

    public static <T> LambdaQueryWrapper<T> lambdaQueryWrapper(){
        return Wrappers.lambdaQuery();
    }
    public static <T> QueryWrapper<T> queryWrapper(){
        return Wrappers.query();
    }

    public <S, T> LambdaQueryWrapper<T> buildWrapper(S request,Class<T> targetClass) {
        QueryWrapper<T> wrapper = buildWrapperWithoutLambda(request,targetClass);
        return wrapper.lambda();
    }

    public <S, T> QueryWrapper<T> buildWrapperWithoutLambda(S request,Class<T> targetClass) {
        if (Objects.isNull(request)) {
            return Wrappers.query();
        }
        QueryTask<S, T> queryTask = new QueryTask<>();
        queryTask.setTClass(targetClass);
        queryTask.setEntity(request);
        queryTask.setWrapper(Wrappers.query());
        queryChainPipeline.start(queryTask);

        return queryTask.getWrapper();
    }

}
